package com.ebelter.temperaturegun.ui.activity;

import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ebelter.btcomlib.CommonLib;
import com.ebelter.btcomlib.common.Constants;
import com.ebelter.btcomlib.models.https.interfaces.HttpResponse;
import com.ebelter.btcomlib.models.https.responses.BaseResponse;
import com.ebelter.btcomlib.utils.TimeUtils;
import com.ebelter.btcomlib.utils.ToastUtil;
import com.ebelter.btcomlib.utils.UserSpUtil;
import com.ebelter.btcomlib.utils.ViewUtils;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.btcomlib.views.TemperatureSelectDialog;
import com.ebelter.btcomlib.views.WeightSelectDialog;
import com.ebelter.btcomlib.views.WheelView;
import com.ebelter.btcomlib.views.dialogs.TextTipDialog;
import com.ebelter.btcomlib.views.dialogs.WheelViewDialog;
import com.ebelter.btcomlib.views.dialogs.picktime2.CustomDatePicker;
import com.ebelter.temperaturegun.R;
import com.ebelter.temperaturegun.model.TgManager;
import com.ebelter.temperaturegun.model.app.TemUtils;
import com.ebelter.temperaturegun.model.http.TGNetUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import static com.ebelter.btcomlib.views.dialogs.TextTipDialog.Gravity_BUTTOM;

public class AddTemperatureActivity extends TBaseActivity implements View.OnClickListener {


    private static final String TAG = "AddTemperatureActivity";
    public static final int REQUEST_CODE = 23;
    public static final int RESULT_CODE = 77;
    private TextView addt_date_tv;
    private TextView addt_time_tv;
    private TextView addt_weizhi_tv;
    private TextView add_djtj;
    private TextView add_add;
    private ImageView top_iv;
    private TextView top_tv1;
    private TextView top_tv2;


    @Override
    protected void initView() {
        addt_date_tv=findViewById(R.id.addt_date_tv);
        addt_time_tv=findViewById(R.id.addt_time_tv);
        addt_weizhi_tv=findViewById(R.id.addt_weizhi_tv);
        top_iv=findViewById(R.id.top_iv);
        top_tv1=findViewById(R.id.top_tv1);
        top_tv2=findViewById(R.id.top_tv2);
        ViewUtils.displayView(top_tv2);
        ViewUtils.displayView(top_tv1);
        ViewUtils.setTextViewStr(top_tv2,CommonLib.getString(R.string.baocun));
        ViewUtils.setTextViewStr(top_tv1,CommonLib.getString(R.string.tjwd));

        add_djtj=findViewById(R.id.add_djtj);
        add_add=findViewById(R.id.add_add);

        addt_date_tv.setOnClickListener(this);
        addt_time_tv.setOnClickListener(this);
        addt_weizhi_tv.setOnClickListener(this);
        top_iv.setOnClickListener(this);
        top_tv2.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        String time = TimeUtils.getCurrentTime1();
        ViewUtils.setTextViewStr(addt_date_tv,time.substring(0,10));
        ViewUtils.setTextViewStr(addt_time_tv,time.substring(11,16));
        updateWeiZhi();
    }

    @Override
    protected int setContentViewId() {
        return R.layout.activity_add_temperature;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.addt_date_tv:
                dateClick();
                break;
            case R.id.addt_time_tv:
                timeClick();
                break;
            case R.id.addt_weizhi_tv:
                weizhiClick();
                break;
            case R.id.top_iv:
                finish();
                break;
            case R.id.top_tv2:
                complte();
                break;
        }
    }


    private long lastUploadTime;
    //完成
    private void complte() {
        if (selectTem<30){
            ToastUtil.show(CommonLib.getString(R.string.qxxzwdzh));
            return;
        }
        long userId = TemUtils.getUerID();
        //00：耳温C标记
        //01：耳温F标记
        //02：额温C标记
        //03：额温F标记
        if (userId>0){


            float temV=selectTem;
            if (!isShshd){
                temV = (float) TgManager.fahrenheit2Centigrade(temV,2);
            }



            int tempType;//  //tempType:温度类型(1:耳温,2:额温),不传默认耳温
            if (weizhiIndex1==2){//weizhiIndex1==1 额温 ==2耳温
                tempType=1;
            }else {
                tempType=2;
            }


            String time = addt_date_tv.getText().toString()+" "+addt_time_tv.getText().toString()+":00";
            LogUtils.i(TAG,"添加温度 time ="+time);


            if (System.currentTimeMillis()-lastUploadTime<2000){
                return;
            }
            lastUploadTime=System.currentTimeMillis();
            TGNetUtils.getInstance().uploadTem(this, userId, temV,tempType, time, new HttpResponse<BaseResponse>() {
                @Override
                public void result(boolean isRequestSuccess, String requestDes, BaseResponse baseResponse, String s) {
                    LogUtils.i(TAG,"uploadTem s = "+s);
                    if (isRequestSuccess&&baseResponse!=null&&baseResponse.resultCode==0){
                        LogUtils.i(TAG,"TgUpLoadDataUtil 数据上传 成功 ");
                        ToastUtil.show(R.string.shchchg);
                        setResult(RESULT_CODE);
                        finish();
                    }else {
                        LogUtils.i(TAG,"TgUpLoadDataUtil 数据上传 失败");
                        ToastUtil.show(R.string.shchshb);
                    }
                }
            });
        }


    }

    private int weizhiIndex1 = 1;//1额温 2耳温
    private int weizhiIndex2 = 1;
    //位置选择
    private void weizhiClick() {
        weizhiIndex2 = weizhiIndex1;
        WheelViewDialog wheelViewDialog = new WheelViewDialog(this);
        wheelViewDialog.setGravity(Gravity_BUTTOM);
        List<String> list=new ArrayList<>();
        list.add(CommonLib.getString(R.string.ewen));
        list.add(CommonLib.getString(R.string.erwen));
        wheelViewDialog.setOffeset(1);
        wheelViewDialog.setItems(list);
        wheelViewDialog.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
            @Override
            public void onSelected(int selectedIndex, String item) {
                Log.i(TAG, "selectedIndex: " + selectedIndex + ", item: " + item);
                weizhiIndex2 = selectedIndex;
            }
        });
        wheelViewDialog.setListener(new TextTipDialog.ICancelOkBtListener() {
            @Override
            public void cancel(Dialog dialogView) {
                Log.i(TAG, "cancel");
                dialogView.cancel();
            }

            @Override
            public void ok(Dialog dialogView) {
                Log.i(TAG, "ok");
                dialogView.cancel();
                weizhiIndex1=weizhiIndex2;
                updateWeiZhi();

            }
        });
        wheelViewDialog.setSelectPosition(weizhiIndex1-1);
        wheelViewDialog.show();

        wheelViewDialog.setBackgroundColor(Color.parseColor("#0176ff"));
    }

    private void updateWeiZhi() {
        ViewUtils.setTextViewStr(addt_weizhi_tv,weizhiIndex1==1?CommonLib.getString(R.string.ewen):CommonLib.getString(R.string.erwen));
    }

    private String dateStr;
    private String timeStr;
    //日期
    private void dateClick() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
        String time = sdf.format(new Date());
        String date = time.split(" ")[0];
        CustomDatePicker datePicker= new CustomDatePicker(this,new CustomDatePicker.ResultHandler() {
            @Override
            public void handle(String time) {
                LogUtils.i(TAG,time);
                //dateStr = time.substring(0,10);
                dateStr = time;

            }
        }, "1945-01-01 00:00", time);
        // datePicker.setSelectedCalender("1991-06-13");
        datePicker.showSpecificTime(false); //显示时和分
        datePicker.setIsLoop(false);
        datePicker.setDayIsLoop(true);
        datePicker.setMonIsLoop(true);
        datePicker.show(date);

        dateStr = TimeUtils.getCurrentTime1();
        datePicker.setSelectedTime(dateStr);


        datePicker.setBackgroundColor(Color.parseColor("#0176ff"));
        datePicker.setListener(new TextTipDialog.ICancelOkBtListener() {
            @Override
            public void cancel(Dialog dialogView) {}//内部已经处理了

            @Override
            public void ok(Dialog dialogView) {

                ViewUtils.setTextViewStr(addt_date_tv,dateStr.substring(0,10));

            }
        });
    }
    //时间
    private void timeClick() {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
        String time = sdf.format(new Date());
        String date = time.split(" ")[0];
        CustomDatePicker datePicker= new CustomDatePicker(this,new CustomDatePicker.ResultHandler() {
            @Override
            public void handle(String time) {
                LogUtils.i(TAG,time);
                //dateStr = time.substring(0,10);
                timeStr = time;

            }
        }, "1945-01-01 00:00", time);
        // datePicker.setSelectedCalender("1991-06-13");
        datePicker.showSpecificTime2(); //显示时和分
        datePicker.setIsLoop(false);
        datePicker.setDayIsLoop(true);
        datePicker.setMonIsLoop(true);
        datePicker.show(date);

        timeStr = TimeUtils.getCurrentTime1();
        datePicker.setSelectedTime(timeStr);


        datePicker.setBackgroundColor(Color.parseColor("#0176ff"));
        datePicker.setListener(new TextTipDialog.ICancelOkBtListener() {
            @Override
            public void cancel(Dialog dialogView) {}//内部已经处理了

            @Override
            public void ok(Dialog dialogView) {
                // birth_cb6.setTitle2(birthday);
                //if (birth_cb6!=null){birth_cb6.setTitle2(birthday);}

                ViewUtils.setTextViewStr(addt_time_tv,timeStr.substring(11,16));


            }
        });
    }

    float selectTem;
    boolean isShshd;

    //添加温度
    public void addTemperature(View view) {

        //0代表°C 1代表°F
        int unit = TemUtils.getUerUnit();
        isShshd = (unit == 0);//摄氏度标志


        TemperatureSelectDialog weightSelectDialog = new TemperatureSelectDialog(this,isShshd);


        selectTem=36.5f;

        if (!isShshd){
            selectTem=97.7f;
        }

        weightSelectDialog.show(selectTem);
        weightSelectDialog.setValueChange(new TemperatureSelectDialog.ValueChange() {
            @Override
            public void change(float value) {
                //LogUtils.i(TAG,"--TemperatureSelectDialog--setValueChange--value = "+value);
                selectTem = value;
            }
        });

        weightSelectDialog.setBackgroundColor(getResources().getColor(R.color.themeblack));
        weightSelectDialog.setListener(new TextTipDialog.ICancelOkBtListener() {
            @Override
            public void cancel(Dialog dialogView) {}//内部已经处理了

            @Override
            public void ok(Dialog dialogView) {
                LogUtils.i(TAG,"--TemperatureSelectDialog--setValueChange--value = "+selectTem);
                String unit = isShshd?" °C":" °F";
                ViewUtils.setTextViewStr(add_djtj,selectTem+unit);
                ViewUtils.setTextColor(add_djtj,Color.parseColor("#ffffff"));
                ViewUtils.goneView(add_add);

            }
        });
        weightSelectDialog.setBackgroundColor(Color.parseColor("#0176ff"));


    }
}
